import 'package:easy_chat_app/commons/GlobalStatus.dart';
import 'package:easy_chat_app/commons/model/GetChatGroupDetailResultData.dart';
import 'package:easy_chat_app/commons/utils/RouteUtils.dart';
import 'package:easy_chat_app/commons/widget/UserHeadPortrait.dart';
import 'package:easy_chat_app/mobile/constants/MobileAppGlobalConstants.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:provider/provider.dart';

class MobileChatGroupUserListPageParams {
  GetChatGroupDetailResultData chatGroupDetail;

  MobileChatGroupUserListPageParams.create({required this.chatGroupDetail});
}

class MobileChatGroupUserListPageChangeNotifier extends ChangeNotifier {}

class MobileChatGroupUserListPage extends StatelessWidget {
  final Logger logger = Logger();

  MobileChatGroupUserListPage({super.key});

  @override
  Widget build(BuildContext context) {
    RouteSettings routeSettings = RouteUtils.currentRouteSettings();
    MobileChatGroupUserListPageParams pageParams =
        routeSettings.arguments as MobileChatGroupUserListPageParams;
    return Consumer<AppUserDetailChangeNotifier>(
        builder: (context, appUserDetailChangeNotifier, child) {
      return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (context) {
            var result = MobileChatGroupUserListPageChangeNotifier();
            return result;
          })
        ],
        builder: (context, child) {
          GetChatGroupDetailResultData chatGroupDetail =
              pageParams.chatGroupDetail;
          int userCount = chatGroupDetail.chatGroupUserItemList?.length ?? 0;
          return Scaffold(
            appBar: AppBar(
              title: Text("群成员（$userCount）"),
              centerTitle: true,
              surfaceTintColor: MobileAppGlobalConstants.appBarSurfaceTintColor,
              backgroundColor: MobileAppGlobalConstants.appBarBackgroundColor,
            ),
            body: Container(
              color: Colors.white,
              height: double.infinity,
              width: double.infinity,
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Container(
                      color: Colors.white,
                      padding:
                          const EdgeInsets.only(top: 10, left: 10, right: 10),
                      child: GridView.count(
                        crossAxisCount: 5,
                        shrinkWrap: true,
                        physics: const NeverScrollableScrollPhysics(),
                        children: userHeadPortraitWidgetList(
                            context, chatGroupDetail),
                      ),
                    ),
                    const SizedBox(
                      height: 100,
                    )
                  ],
                ),
              ),
            ),
          );
        },
      );
    });
  }

  List<Widget> userHeadPortraitWidgetList(
      BuildContext context, GetChatGroupDetailResultData chatGroupDetail) {
    final screenSize = MediaQuery.of(context).size;
    double iconSize = (screenSize.width - 40) / 5 - 20;
    List<Widget> userHeadPortraitWidgetList =
        (chatGroupDetail.chatGroupUserItemList ?? []).map((item) {
      return Column(
        children: [
          Container(
              child: UserHeadPortrait.network(
                  size: iconSize,
                  userCode: item.code!,
                  userHeadPortrait: item.headPortrait!,
                  userHeadPortraitStoreType: item.headPortraitStoreType!,
                  circularRadius: 5)),
          Text(
            "${item.nickname}",
            style: const TextStyle(
                fontSize: 12, color: Color.fromRGBO(115, 115, 115, 1)),
          )
        ],
      );
    }).toList();
    userHeadPortraitWidgetList.add(Column(
      children: [
        Expanded(
          child: ImageIcon(
            const AssetImage("images/加号.png"),
            size: iconSize,
            color: const Color.fromRGBO(178, 178, 178, 1),
          ),
        ),
        const Text("")
      ],
    ));
    return userHeadPortraitWidgetList;
  }
}
